package pub.tbc.toolkit;

import com.google.common.collect.Maps;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * @author tbc on 2016/8/25 0:43.
 */
public class DateFormats {

    private DateFormats() {
        throw new AssertionError("No pub.tbc.toolkit.DateFormats instances for you!");
    }

    public static String FORMAT_DEFAULT = "yyyy-MM-dd HH:mm:ss";
    public static String FORMAT_ALL = "yyyy-MM-dd HH:mm:ss.SSS";
    public static String FORMAT_YMD = "yyyy-MM-dd";
    public static String FORMAT_HMD = "HH:mm:ss";
    public static String FORMAT_Y = "yyyy";
    public static String FORMAT_MM = "MM";
    public static String FORMAT_D = "dd";
    public static String FORMAT_H = "HH";
    public static String FORMAT_MI = "mm";
    public static String FORMAT_S = "ss";

    private static final ThreadLocal<Map<String, DateFormat>> fms = new ThreadLocal<>();

    public static DateFormat getFormat(String format) {
        Map<String, DateFormat> map = fms.get();

        if (map == null) {
            map = Maps.newHashMap();
            fms.set(map);
        }

        if (Objs.isEmpty(format)) {
            format = FORMAT_DEFAULT;
        }

        DateFormat ret = map.get(format);

        if (ret == null) {
            ret = new SimpleDateFormat(format);
            map.put(format, ret);
        }

        return ret;
    }

    public static DateFormat getFormat() {
        return getFormat(null);
    }

    public static DateTimeFormatter getJotaFormater() {
        return getJotaFormater(FORMAT_DEFAULT);
    }

    public static DateTimeFormatter getJotaFormater(String format) {
        return DateTimeFormat.forPattern(format);
    }


}
